package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.SysMsgAdapter;
import com.xgkj.diyiketang.data.MessageInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.task.SysMsgListTask;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;

import java.util.List;

/**
 * Created by Administrater on 2015/12/25.
 */
public class SysMsgListActivity extends BaseActivity implements PullToRefreshListView.OnRefreshListener,PullToRefreshListView.OnLoadDataListener,AdapterView.OnItemClickListener {
    private Context mContext;
    private PullToRefreshListView msgList;
    private SysMsgAdapter msgAdapter;
    private int currentPage = 1;
    private TextView back;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sysmsglist);
        mContext = this;
        initView();
        getSysMsgList();
    }

    private void initView(){

        msgList = (PullToRefreshListView) findViewById(R.id.sysmsg_list);
        msgList.init(mContext,true);
        msgList.setOnRefreshListener(this);
        msgList.setOnLoadDataListener(this);
        msgList.setOnItemClickListener(this);
        msgAdapter = new SysMsgAdapter(mContext);
        msgList.setAdapter(msgAdapter,true);
        back = (TextView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
       // msgList.doRefresh();
    }

    private void getSysMsgList(){
        if(currentPage ==0){
            msgList.setNoNextPage();

        }
        SysMsgListTask task = new SysMsgListTask(10,currentPage);
        task.addListener(new SysMsgListner(mContext));
        getEatShowApp().getTaskManager().addTask(task);
    }

    @Override
    public void onRefresh() {
        currentPage =1;
        getSysMsgList();
    }

    @Override
    public void onPullRefresh() {

    }

    @Override
    public void onLoadMore() {
        getSysMsgList();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {
        if(msgList.getHeaderViewsCount()>0){
            --position;
        }
        if (position<0 || position>msgAdapter.getCount())return;
        if (position >= 0 && position < msgAdapter.getCount()) {
            MessageInfo info = (MessageInfo) msgAdapter.getItem(position);
            Intent intent = new Intent(mContext, SysMsgDetailActivity.class);
            intent.putExtra("ID", info.getMsgId());
            startActivity(intent);
        }
    }

    class SysMsgListner extends ShowToastTaskEventListener {
        public SysMsgListner(Context context) {
            super(context);
        }

        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if(data!=null) {
                if(currentPage == 1){
                    msgAdapter.clearData();
                    msgList.enableLoadMore();
                    msgList.onRefreshComplete();
                }else{
                    msgList.onLoadDataComplete(true);
                }
                List<MessageInfo> list = (List<MessageInfo>) data;
                if(list!=null&&list.size()>0){
                    msgAdapter.updateData(list);
                }
                currentPage = result;
            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if(currentPage == 1){
                msgList.onRefreshComplete();
            }else{
                msgList.onLoadDataComplete(false);
            }
        }
    }
}
